<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //解构赋值
        //ES5
        // let arr = ["张三", "李四", "王五"];
        // let a = arr[0];
        // let b = arr[1];
        // let c = arr[2];
        // console.log(a, b, c);//张三 李四 王五


        //ES6 数组的解构赋值
        // let arr = ["张三", "李四", "王五"];
        // let [a, b, c] = arr;
        // console.log(a, b, c);//张三 李四 王五


        // ES5 交换变量值
        // var a = 10;
        // var b = 20;
        // var temp = a;
        // a = b;
        // b = temp;
        // console.log(a, b); // 20 10

        //ES6 交换变量值
        // var a = 10;
        // var b = 20;
        // var [b, a] = [a, b];
        // console.log(a, b); // 20 10


        // 解构赋值之默认值
        // function fn([a, b, c = "默认值"]) {
        //     console.log(a, b, c); //张三 李四 王五
        // }
        // fn(["张三", "李四", "王五"])
        // let [a, b, c = "默认"] = ["张三", "李四", "王五"]
        // console.log(a, b, c); //张三 李四 王五


        //解构复杂数据练习
        // let arr = [1, 2, [4, 5, [6, 7]]];
        // let [a, b, [c, d, [e, f]]] = arr;
        // console.log(a, b, c, d, e, f); //1 2 4 5 6 7


        //对象的解构
        // let obj = {
        //     name: "张三",
        //     age: 20,
        //     height: "178cm"
        // }
        // let {name:myname,age:myage,height:myheight} = obj;
        // console.log(myname,myage,myheight);
        // let {name:name,age:age,height:height} = obj;
        // ES6对象里 对象键名和键值一样可简写为一个
        // let {
        //     name,
        //     age,
        //     height
        // } = obj;
        // console.log(name, age, height);//张三 20 178cm


        //解构复杂数据练习
        let obj = {
            name: "张小三",
            hobby: ["张三", "李四", "王五"],
            obj1: {
                myname: "王健林",
                age: 60
            }
        }

        let {
            name,
            hobby:[a,b,c],
            obj1:{myname,age}
        } = obj;
        console.log(obj);
    </script>
</body>

</html>